#!/usr/bin/env python
# coding: utf-8

# In[ ]:


def record_score():
    name = input("请输入学生姓名：")
    student_id = input("请输入学生学号：")
    course = input("请输入课程名称：")
    while True:
        try:
            score = float(input("请输入成绩（0-100）："))
            if 0 <= score <= 100:
                break
            else:
                print("成绩必须在0到100之间，请重新输入。")
        except ValueError:
            print("成绩输入无效，请输入有效的数字成绩。")
    students.append({"name": name, "id": student_id, "course": course, "score": score})
    print("成绩已成功记录！")


def query_score():
    print("请选择查询方式：")
    print("1. 按学生姓名查询")
    print("2. 按学生学号查询")
    print("3. 按课程名称查询")
    choice = input("请输入选项序号：")
    if choice == "1":
        name = input("请输入学生姓名：")
        found = False
        for student in students:
            if student["name"] == name:
                print(f'姓名：{student["name"]}，学号：{student["id"]}，课程：{student["course"]}，成绩：{student["score"]}')
                found = True
        if not found:
            print("未找到该学生的成绩记录。")
    elif choice == "2":
        student_id = input("请输入学生学号：")
        found = False
        for student in students:
            if student["id"] == student_id:
                print(f'姓名：{student["name"]}，学号：{student["id"]}，课程：{student["course"]}，成绩：{student["score"]}')
                found = True
        if not found:
            print("未找到该学生的成绩记录。")
    elif choice == "3":
        course = input("请输入课程名称：")
        found = False
        for student in students:
            if student["course"] == course:
                print(f'姓名：{student["name"]}，学号：{student["id"]}，课程：{student["course"]}，成绩：{student["score"]}')
                found = True
        if not found:
            print("未找到该课程的成绩记录。")
    else:
        print("无效的查询选项，请重新运行查询功能。")


def statistics_score():
    course = input("请输入课程名称：")
    scores = [student["score"] for student in students if student["course"] == course]
    if scores:
        avg_score = sum(scores) / len(scores)
        max_score = max(scores)
        min_score = min(scores)
        print(f'课程：{course}')
        print(f'平均分：{avg_score:.2f}')
        print(f'最高分：{max_score}')
        print(f'最低分：{min_score}')
    else:
        print("未找到该课程的成绩记录。")


def main_menu():
    while True:
        print("欢迎使用学生成绩管理系统")
        print("请选择操作：")
        print("1. 记录学生成绩")
        print("2. 查询学生成绩")
        print("3. 统计课程成绩")
        print("4. 退出系统")
        choice = input("请输入选项序号：")
        if choice == "1":
            record_score()
        elif choice == "2":
            query_score()
        elif choice == "3":
            statistics_score()
        elif choice == "4":
            print("感谢使用学生成绩管理系统，再见！")
            break
        else:
            print("无效的选项，请重新输入。")


students = []
if __name__ == "__main__":
    main_menu()


# In[ ]:




